from selenium.webdriver.remote.webdriver import WebElement

from utils.selenium.find import FindElement
from utils.selenium import highlighted
from utils.selenium.perform import WPerform


def find_element(perform: WPerform, item: dict) -> WebElement or list:
    """查询"""
    find_type = item.get('find_type')
    find_key = item.get('find_key')
    find_multiple = item.get('find_multiple', False)
    find_multiple_index = item.get('find_multiple_index', 0)
    find_multiple_index = find_multiple_index - 1 if find_multiple_index >= 1 else None
    is_filter_hide_element = item.get('is_filter_hide_element', True)
    # 查询方法
    find_data = FindElement.find_element(
        driver=perform.driver,
        find_type=find_type,
        find_key=find_key,
        multiple=find_multiple,
        highlighted=False,
        find_index=find_multiple_index,
        is_filter_hide_element=is_filter_hide_element
    )
    if not find_data:
        find_type_name = item.get('find_type_name')
        raise Exception("Dom元素未找到 ==> 查询类型【%s】查询关键字【%s】查询多个【%s】" % (find_type_name, find_key, '是' if find_multiple else '否'))
    if type(find_data) == list:
        multiple_option_interval = item.get('multiple_option_interval')
        if not multiple_option_interval or multiple_option_interval == 0:
            highlighted.ElementHighlighted.highlighted(perform, find_data)
    else:
        highlighted.ElementHighlighted.highlighted(perform, find_data)
    return find_data




